import EnemyData from "../EnemyData";
import StageScene from "./StageScene";
import Enemy from "./Enemy";
export default class EnemySpawner extends Laya.Script3D {
  private spawner: Laya.Sprite3D
  public static instance: EnemySpawner
  public waves: Array<EnemyData> = [
    {
      count: 3,
      modelPath: 'res/atlas/Models/Conventional/Enemy0.lh',
      interval: 1000,
      enmey: null
    }, {
      count: 6,
      modelPath: 'res/atlas/Models/Conventional/Enemy1.lh',
      interval: 800,
      enmey: null
    }, {
      count: 9,
      modelPath: 'res/atlas/Models/Conventional/Enemy2.lh',
      interval: 600,
      enmey: null
    }, {
      count: 12,
      modelPath: 'res/atlas/Models/Conventional/Enemy3.lh',
      interval: 500,
      enmey: null
    },
  ]
  private waveIndex;
  private count;
  onEnable() {
    this.waveIndex = 0
    // 创建敌人 
    this.spawner = this.owner as Laya.Sprite3D
    this.spawner.active = false
    this.count = 0
    EnemySpawner.instance = this
    this.createEnemy()
  }

  private createEnemy() {
    let wave = this.waves[this.waveIndex]
    if (!wave) return
    this.createEnemyDetail()
  }

  public judgeWaveDown() {
    this.count--
    if (this.count <= 0) {
      this.createEnemy()
    }
  }

  private createEnemyDetail() {
    let wave = this.waves[this.waveIndex]
    wave.count--
    if (wave.count < 0) {
      this.waveIndex++
      Laya.timer.clear(this, this.createEnemyDetail)
      return
    }
    if (wave.enmey) {
      var enmey = Laya.Sprite3D.instantiate(wave.enmey, StageScene.instance.stageScene, false, this.spawner.transform.position)
      this.count++
    } else
      // 创建对象
      Laya.Sprite3D.load(wave.modelPath, Laya.Handler.create(this, (ms: Laya.Sprite3D) => {
        ms.name = this.waveIndex
        let collider = ms.getComponent(Laya.PhysicsCollider) as Laya.PhysicsCollider
        collider.collisionGroup = Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER8;
        // 可碰撞组
        // collider.canCollideWith =  Laya.Physics3DUtils.COLLISIONFILTERGROUP_CUSTOMFILTER6
        ms.addComponent(Enemy)
        wave.enmey = ms
        wave.count++
        Laya.timer.loop(wave.interval, this, this.createEnemyDetail)
      }))
  }
}